Examples:

Controlling graph size

 # Load Libraries
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.4
## ✔ tidyr   1.0.2     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
# Load Files 
SNPs<- read.table("23andMe_complete.txt", header = TRUE, sep = "\t")
# to adjust figure size {r, fig.width = 6, fig.height = 6}
SNPs$chromosome = ordered(SNPs$chromosome, levels=c(seq(1, 22), "X", "Y", "MT"))
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = genotype, fill = chromosome)) + 
  coord_polar() +
  ggtitle("Total SNPs for each genotype") +
  ylab("Total number of SNPs") +
  xlab("Genotype")

Grapgic Output

# Plot graph to a pdf outputfile
pdf("SNP_example_plot.pdf", width=6, height=3)
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = chromosome, fill = genotype))
dev.off()
## png 
##   2
# Plot graph to a png outputfile
ppi <- 300
png("SNP_example_plot.png", width=6*ppi, height=6*ppi, res=ppi)
ggplot(data = SNPs) + 
  geom_bar(mapping = aes(x = chromosome, fill = genotype))
dev.off()
## png 
##   2

RMarkdown loading images

# This is the RMarkdown style for inserting images
# Your image must be in your working directory
# This command is put OUTSIDE the r code chunk
Genotype counts per chromosome

Genotype counts per chromosome

# This is an alternative way using html. 
# Remember that it must be in your working directory or you will need to specify the full path.
# The html is put OUTSIDE the r code chunk.

Genotype counts per chromosome

Another way to present a graph without the code is adding echo = FALSE within the r{} chunk - {r echo = FALSE}. This prevents code, but not the results from appearing in the knitr file.

Interactive graphs and tables ni RMarkdown reports

# Version 1 1
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + 
  geom_point()
ggplotly(p)
# Version 2
library(plotly)
ggplotly(
  ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + 
    geom_point()
 )

DT can make interactive tables

library(DT)
datatable(iris)

Exercises:

Exercise 1

e1 <- ggplot(SNPs,aes(SNPs$chromosome,color=SNPs$chromosome))+geom_bar(fill="steelblue", color="steelblue")
e1+ggtitle("Genes on Chromosomes") + xlab("Chromosome") + ylab("Number of genes on chromosome")

Exercise 2:

e2<-ggplot(SNPs,aes(SNPs$chromosome,fill=SNPs$genotype))+geom_bar()
e2+ggtitle("Genes on Chromosomes") + xlab("Chromosome") + ylab("Number of genes on chromosome") + scale_fill_manual("legend", values = c("A"="skyblue", "T"="green", "C"="deeppink", "G"="black", "AA"="red", "AT"="cyan", "CG"="darkgreen", "TT"="salmon", "CC"="gold", "GG"="grey", "AC"="blue4", "CT"="brown4", "GT"="azure", "AG"= "orange", "D"="white", "DD"="blue", "I"="coral", "II"="purple", "DI" = "pink", "--" = "yellow"))

Exersice 3

Genotype counts per chromosome

Genotype counts per chromosome

Exersice 4

f <- ggplot(SNPs,aes(SNPs$chromosome,fill=SNPs$genotype))+geom_bar()
f+facet_wrap(~SNPs$genotype)+ggtitle("Genes on Chromosomes") + xlab("Chromosome") + ylab("Number of genes on chromosome")+theme(axis.text.x=element_text(angle=90,hjust=.05,vjust=0.05,size=5))

Exersice 5

library(plotly)
f <- ggplot(SNPs,aes(SNPs$chromosome))+geom_bar()
f1<-f+facet_wrap(~SNPs$genotype)+ggtitle("Genes on Chromosomes") + xlab("Chromosome") + ylab("Number of genes on chromosome")
ggplotly(f1)

Exersice 6

library(DT)
datatable(subset(SNPs, chromosome == 'Y'))
## Warning in instance$preRenderHook(instance): It seems your data is too
## big for client-side DataTables. You may consider server-side processing:
## https://rstudio.github.io/DT/server.html